package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.UserService;
import util.MD5;

public class UserServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UserServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		String userName = request.getParameter("userName");
		String email = request.getParameter("email");

		try {
			out.print(UserService.checkUserByEmail(userName, email) == true ? "failed"
					: "success");
			// System.out.println(UserService.checkUserByEmail(userName,email)==true?"failed":"success");
		} catch (SQLException e) {
			e.printStackTrace();
		}

		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		PrintWriter out = response.getWriter();

		// 验证邮箱是否已被注册
		if ("valide".equals(action)) {
			String userName = request.getParameter("userName");
			String email = request.getParameter("email");
			try {
				out.print(UserService.checkUserByEmail(userName, email) ? "failed"
						: "success");
				// System.out.println(UserService.checkUserByEmail(userName,email)==true?"failed":"success");
			} catch (SQLException e) {
				e.printStackTrace();
			}
			// 用户修改信息
		} else if ("changeInfo".equals(action)) {
			String userName = request.getParameter("userName");
			String addr = request.getParameter("addr");
			String sex = request.getParameter("sex");
			String email = request.getParameter("email");
			String campus = request.getParameter("campus");
			String other = request.getParameter("other");
			try {
				out.print(UserService.changeUser(userName, sex, addr, email,
						campus, other));
				// System.out.println(UserService.checkUserByEmail(userName,email)==true?"failed":"success");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if ("changePassword".equals(action)) {
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			try {
				out.print(UserService.changeUserPassword(userName,
						MD5.Encrypt(password)));
				// System.out.println(UserService.checkUserByEmail(userName,email)==true?"failed":"success");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if ("changePhone".equals(action)) {
			String userName = request.getParameter("userName");
			String phone = request.getParameter("phone");
			try {
				out.print(UserService.changeUserPhone(userName, phone));
				// System.out.println(UserService.checkUserByEmail(userName,email)==true?"failed":"success");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
